package com.ds.infrastructure.hyperspace.console.schedule;

import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbMethodLock;
import com.ds.infrastructure.hyperspace.console.entity.where.TbMethodLockCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.IMethodLockService;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-11-06 16:34
 **/
@Slf4j
@Component
public class MethodLockJob {
    @Autowired
    IMethodLockService iMethodLockService;

    /**
     * 执行周期：每一分钟执行一次
     * 删除超时的方法锁，超时时间3分钟
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void doWork() {
        Date now = new Date();
        TbMethodLockCustomWhere customWhere = new TbMethodLockCustomWhere().setUpdateTime4EndTime(DateUtils.addMinutes(now, -3));
        List<TbMethodLock> tbMethodLockList = iMethodLockService.getList(null, customWhere, TbPageWhere.newPage4All());
        if (tbMethodLockList == null || tbMethodLockList.isEmpty()) {
            return;
        }
        for (TbMethodLock tbMethodLock : tbMethodLockList) {
            iMethodLockService.delete(tbMethodLock.getId());
            log.info("清理超时方法锁，超时时间3分钟:" + FastJsonUtil.serialize(tbMethodLock));
        }
    }
}
